package com.coocaa.x.service.litedb.data.common.obj;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import android.content.ContentValues;

import com.coocaa.x.framework.data.JObject;

/**
 * Created by lu on 15-3-12.
 */
public class KVMap extends JObject {
    private LinkedHashMap<String, String> map = null;

    public KVMap() {
        map = new LinkedHashMap<String, String>();
    }

    public KVMap(LinkedHashMap<String, String> map) {
        this.map = map;
    }

    public synchronized void put(String key, String value) {
        map.put(key, value);
    }

    public synchronized List<String> getKeys() {
        List<String> ret = new ArrayList<String>();
        for (String o : map.keySet())
            ret.add(o);
        return ret;
    }

    public synchronized List<String> getValues() {
        List<String> ret = new ArrayList<String>();
        for (String o : map.keySet())
            ret.add(map.get(o));
        return ret;
    }

    public synchronized String getValue(String key) {
        return map.get(key);
    }

    public synchronized String[] toKeysArray() {
        String[] ret = new String[map.size()];
        List<String> keys = getKeys();
        keys.toArray(ret);
        return ret;
    }

    public synchronized String[] toValuesArray() {
        String[] ret = new String[map.size()];
        List<String> values = getValues();
        values.toArray(ret);
        return ret;
    }

    public synchronized ContentValues toContentValues() {
        ContentValues cv = new ContentValues();
        List<String> keys = getKeys();
        for (String key : keys)
            cv.put(key, getValue(key));
        return cv;
    }
}
